﻿//
// XN4SL - Xna Framework for Silverlight.
//
using System;
using System.Windows.Controls;
using System.Windows;
using System.IO;
using System.Windows.Media.Imaging;

namespace Xn4Sl.Framework.Graphics
{
    /// <summary>
    /// Queries and prepares resources.
    /// </summary>
    public abstract class GraphicsResource : IDisposable
    {
        /// <summary>
        /// Image element.
        /// </summary>
        protected Image image;

        /// <summary>
        /// Gets the GraphicsDevice associated with this GraphicsResource.
        /// </summary>
        public virtual GraphicsDevice GraphicsDevice 
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// Gets a value that indicates whether the object is disposed.
        /// </summary>
        public bool IsDisposed 
        { 
            get 
            { 
                return this.isDisposed; 
            } 
        }
        bool isDisposed;

        /// <summary>
        /// Gets the name of the resource.
        /// </summary>
        public string Name { get; set; }
        
        /// <summary>
        /// Gets or sets the resource-management priority for this resource.
        /// </summary>
        public int Priority { get; set; }
        
        /// <summary>
        /// Gets the resource tags for this resource.
        /// </summary>
        public object Tag { get; set; }

        /// <summary>
        /// Specifies the display state of an element.
        /// </summary>
        internal Visibility Visible
        {
            get
            {
                return image.Visibility;
            }
            set
            {
                image.Visibility = value;
            }
        }

        /// <summary>
        /// Occurs when Dispose is called or when this object is finalized and collected
        /// by the garbage collector of the Microsoft .NET common language runtime.
        /// </summary>
        public event EventHandler Disposing;
        
        /// <summary>
        /// Immediately releases the unmanaged resources used by this object.
        /// </summary>
        public void Dispose()
        {
            if (this.image != null)
            {
                if (this.image.Parent != null)
                {
                    ((Panel)this.image.Parent).Children.Remove(this.image);
                }
                this.image.Source = null;
                this.image = null;
            }
            this.isDisposed = true;
            this.raise_Disposing(this, null);
        }

        /// <summary>
        /// Immediately releases the unmanaged resources used by this object.
        /// </summary>
        /// <param name="__p1">true to release both managed and unmanaged resources; 
        /// false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool __p1)
        {
            this.Dispose();
        }

        /// <summary>
        /// Raises the Disposing event when called from within a derived class.
        /// </summary>
        /// <param name="value0">Invoking object reference; should be this object.</param>
        /// <param name="value1">Arguments to pass to the event handler.</param>
        protected void raise_Disposing(object value0, EventArgs value1)
        {
            if (this.Disposing != null) this.Disposing(value0, value1);
        }
    }
}
